package jsyf.com.util;

/**
 * Created by qiaox on 2017/10/24.
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @description: json转化工具
 * @author: @崔程鵬 @cuichengpeng
 * @creat: 13:51  2017/10/24
 * @since: V1.0.0
 * @see jsyf.oa.web
 */
public class JsonUtils {
    public final static void convert(Object json) {
        if (json instanceof List) {
            List arr = (List) json;
            for (Object obj : arr) {
                convert(obj);
            }
        } else if (json instanceof JSONObject) {
            JSONObject jo = (JSONObject) json;
            Set<String> keys = jo.keySet();
            String[] array = keys.toArray(new String[keys.size()]);
            for (String key : array) {
                Object value = jo.get(key);
                String[] key_strs = key.split("_");
                if (key_strs.length > 1) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < key_strs.length; i++) {
                        String ks = key_strs[i];
                        if (!"".equals(ks)) {
                            if (i == 0) {
                                sb.append(ks);
                            } else {
                                int c = ks.charAt(0);
                                if (c >= 97 && c <= 122) {
                                    int v = c - 32;
                                    sb.append((char) v);
                                    if (ks.length() > 1) {
                                        sb.append(ks.substring(1));
                                    }
                                } else {
                                    sb.append(ks);
                                }
                            }
                        }
                    }
                    jo.remove(key);
                    jo.put(sb.toString(), value);
                }
                convert(value);
            }
        }
        System.out.println(json);
    }

    public final static Object convert(String json) {
        Object obj = JSON.parse(json);
        convert(obj);
        return obj;
    }
    public static void main(String arg[]) {
        List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        JSONObject jsonObject = new JSONObject();

        for (int i = 0; i <2; i++) {
            jsonObject = new JSONObject();
            jsonObject.put("user_name","userName");
            jsonObject.put("user_id","userId");
            jsonObjects.add(jsonObject);
        }
        convert(jsonObjects);
    }
}
